package io.gitee.chearnee.fw.datasecurity.core.util;

import io.gitee.chearnee.fw.common.utils.ObjectUtil;
import io.gitee.chearnee.fw.datasecurity.core.bean.KeyGenerateReference;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * @author kam
 * @since 2021/4/8
 *
 * <p>
 * 加解密帮助类
 * </p>
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DbCryptUtil {

    /**
     * 线程副本存储ID 用以克隆的对象的ID赋值
     */
    private static final ThreadLocal<KeyGenerateReference> KEY_GENERATE_REFERENCE_THREAD_LOCAL = new ThreadLocal<>();

    public static boolean inIgnoreClass(Class<?> cls) {
        return ObjectUtil.inIgnoreClass(cls);
    }

    /**
     * 放入keyGen的源对象和克隆对象
     *
     * @param originPojo 源对象，即调用插入方法时的入参对象
     * @param clonePojo  克隆对象 为避免原先的代码重复加密而克隆的对象
     */
    public static void setKeyGenerateReference(Object originPojo, Object clonePojo) {
        KeyGenerateReference reference = new KeyGenerateReference();
        reference.setOriginPojo(originPojo);
        reference.setClonePojo(clonePojo);
        KEY_GENERATE_REFERENCE_THREAD_LOCAL.set(reference);
    }

    /**
     * 获取keyGen的引用
     *
     * @return keyGen引用
     */
    public static KeyGenerateReference getKeyGenerateReference() {
        return KEY_GENERATE_REFERENCE_THREAD_LOCAL.get();
    }

    /**
     * 清理keyGen引用
     */
    public static void cleanKeyGenerateReference() {
        KEY_GENERATE_REFERENCE_THREAD_LOCAL.remove();
    }
}
